package com.teamone.etl.dataruleengine.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.dataruleengine.dto.RuleEngineReq;
import com.teamone.etl.dataruleengine.entity.RuleEngine;
import com.teamone.etl.dataruleengine.service.RuleEngineService;
import com.teamone.etl.exception.CommonCode;
import com.teamone.etl.util.NameUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

import static com.teamone.etl.constant.EtlConstant.TypeConstant.CODE_TYPE_JAVA;
import static com.teamone.etl.constant.EtlConstant.TypeConstant.CODE_TYPE_SQL;

/**
 * (RuleEngine)表控制层
 *
 * @author makejava
 * @since 2021-01-12 19:38:55
 */
@RestController
@RequestMapping("ruleEngine")
public class RuleEngineController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private RuleEngineService ruleEngineService;


    /**
     * 自动生成java类名称
     * @return java类名称
     */
    @GetMapping("/getRuleEngineJava")
    public String getRuleEngineJava(){
        return NameUtil.ruleengineJava();
    }

    /**
     * 自动生成sql函数名称
     * @return sql函数名称
     */
    @GetMapping("/getRuleEngineSql")
    public String getRuleEngineSql(){
        return NameUtil.ruleengineSql();
    }

    /**
     * 添加规则
     * @param ruleEngine 规则引擎对象
     * @return  是否添加成功
     * @throws IOException  创建文件发生异常
     */
    @PostMapping("/addRuleEngine")
    public R addRuleEngine(@RequestBody RuleEngine ruleEngine) throws IOException {
        return success(ruleEngineService.addRuleEngine(ruleEngine));
    }

    /**
     * 根据不同类型，编译不同文件
     * @param ruleEngine  规则引擎对象
     * @return  是否成功写入
     */
    @PostMapping("/writer")
    public R writer(@RequestBody RuleEngine ruleEngine) throws IOException {
        //根据id获得当前对象
        RuleEngine re = ruleEngineService.getRuleEngineById(ruleEngine.getId());
        //判断当前对象代码类型    1：java  2：sql
        if(CODE_TYPE_JAVA.equals(re.getCodeTypeId())){
            //编译和运行java文件      根据id获得的对象 和  新编写的代码
            return success(ruleEngineService.compileJava(re,ruleEngine.getCodes()));
        }else if(CODE_TYPE_SQL.equals(re.getCodeTypeId())){
            //编译 执行sql文件        根据id获得的对象 执行sql语句
            return success(ruleEngineService.execute(ruleEngine));
        }else{
            return success(false);
        }
    }

    /**
     * 模糊分页查询
     *
     * @param codeTypeId    规则类型id
     * @param name          规则名称
     * @param functionName  类/函数名称
     * @param page          当前页
     * @param size          每页显示条数
     * @return 部分数据
     */
    @GetMapping("/ruleSearch")
    public R ruleSearch(@Param("codeTypeId") String codeTypeId,
                        @Param("name") String name,
                        @Param("functionName") String functionName,
                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "size",defaultValue = "10") Integer size) {
        PageInfo<RuleEngine> search = ruleEngineService.ruleSearch(codeTypeId, name, functionName, page, size);
        return success(search);
    }

    /**
     *
     * @param id 单条件查询
     * @return  规则引擎对象
     */
    @GetMapping("/get")
    public R<RuleEngine> get(Integer id) {
        RuleEngine byId = ruleEngineService.getRuleEngineById(id);
        return success(byId);
    }

    /**
     *  修改规则引擎
     *
     * @param ruleEngineReq 接收参数
     * @return    成功返回规则引擎对象 失败返回false
     */
    @PutMapping("/updateRuleEngine")
    public R<Object> updateRuleEngine(@RequestBody RuleEngineReq ruleEngineReq) {

        try {
            Boolean aBoolean = ruleEngineService.updateRuleEngine(ruleEngineReq);
            if(aBoolean){
                return success(get(ruleEngineReq.getId()));
            }else{
               return success(CommonCode.IS_NAME_EXISTS);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return  success(false);
        }
    }

    /**
     * 逻辑删除
     * @param id 主键
     * @return   是否成功
     */
    @DeleteMapping("/delRuleEngineById")
    public R<Boolean> delRuleEngineById(Integer id) throws IOException {


        try{
            //逻辑删除 将is_disable字段改为-1
            ruleEngineService.delRuleEngineById(id);
            return success(true);
        } catch (Exception e) {
            e.printStackTrace();
            return success(false);
        }
    }

}